# -*- coding: utf-8 -*-
import wsgiref.handlers
import xmw
import xmw.error

from google.appengine.ext import webapp
from xmw.tianya import tianya_msg_utils

class MsgBuilder():
    pass

class MainPage(xmw.RequestHandler):    
    def post(self):
        msg_name    = self.request.POST['name']
        helper=tianya_msg_utils.get_msg_helper(msg_name)
        
        if helper is None:
            e=xmw.error.UNSUPPORTED_MSG(msg_name)
            self.render_error(e)
            return 
        
        (msg,e)=helper.build_msg(self.request.POST)

        if msg is None:
            self.render_error(e)
            return
        
        #OK
        msg.put()
        e=xmw.error.OK("receive message with name:%s"%msg_name)
        self.render_error(e)
        return 


def main():
  application = webapp.WSGIApplication(
                                       [('/dispatcher/rec_msg', MainPage)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()